package sk.naive.talker.command;

import sk.naive.talker.*;
import sk.naive.talker.props.*;
import sk.naive.talker.adapter.TagConsts;
import sk.naive.talker.message.DefaultMessageFactory;
import sk.naive.talker.message.Category;
import sk.naive.talker.util.Utils;
import sk.naive.talker.persistence.*;

import java.rmi.RemoteException;
import java.util.*;

/**
 * @author <a href="mailto:virgo@naive.deepblue.sk">Richard "Virgo" Richter</a>
 * @version $Revision: 1.31 $ $Date: 2005/02/22 19:09:52 $
 */
public class Read extends AbstractCommand {
	private Board board;
	private List<Text> texts;
	private String boardName;
	private Integer onlyForAuthorId;
	private int startNum = 1;
    public static final String UPROP_TEXT_PAGER = "textPager";

	public void exec()
		throws CommandException, RemoteException, PropertyStoreException
	{
		try {
			String[] sa = Utils.splitWords(params, 3);
			UserFinder uf = commandDispatcher.getUserFinder();
			if (sa.length == 1) {
				boardName = sa[0];
			} else if (sa.length == 2) {
				boardName = sa[0];
				try {
					startNum = Integer.parseInt(sa[1]);
				} catch (NumberFormatException e) {
					onlyForAuthorId = uf.findUserByLogin(sa[1]).getId();
				}
			} else if (sa.length > 2) {
				sendMisusageWarning();
				return;
			}

			BoardHelper bh = new BoardHelper(commandDispatcher, user, boardName);
			board = bh.getBoard();
			if (bh.getBoard() == null) {
				sendHelper().sendMessage(user, bh.getWarningMessageKey());
				return;
			}
			ctxSet(DefaultMessageFactory.CTXKEY_VAL, board.getProperties());

			texts = board.texts();
			if (texts.size() == 0) {
				user.send(getString("board.empty", user.getProperties()));
				return;
			}

			viewBoard();

			ctxSet(DefaultMessageFactory.CTXKEY_VAL, board.getName());
			sendHelper().sendLocal(user, null, "read.audience", Category.LOCAL);
		} catch (PersistenceException e) {
			throw new CommandException(e);
		}
	}

	private void viewBoard() throws PersistenceException, RemoteException, PropertyStoreException {
		int counter = 1;
		StringBuilder sb = new StringBuilder();
		TextPersistence tp = commandDispatcher.textPersistence();
		BoardMessageRights messageRights = new BoardMessageRights(tp);

		sb.append(getString("read.head", user.getProperties()));
		for (Text text : texts) {
			if (counter < startNum) {
				counter++;
				continue;
			}
			Integer authorId = text.getAuthorId(tp);
			if (skipWhenSearchingByAuthor(authorId)) {
				counter++;
				continue;
			}
			ctxSet(DefaultMessageFactory.CTXKEY_VAL + 1, text.getCreated(tp));
			ctxSet(DefaultMessageFactory.CTXKEY_VAL + 2, String.valueOf(counter));
			String messageKey = "read.messageTitle";
			User author = commandDispatcher.getUserFinder().findUserById(authorId);
			if (author != null) {
				ctxSet(DefaultMessageFactory.CTXKEY_VAL, author.getProperties());
			} else {
				ctxSet(DefaultMessageFactory.CTXKEY_VAL, authorId);
				messageKey = "read.messageTitleInvalidAuthor";
			}
			if (messageRights.isWipeable(text, user, board)) {
				messageKey += ".wipeable";
			}
			sb.append(getString(messageKey, user.getProperties()));
			sb.append(text.getContent()).append(Utils.tag(TagConsts.BR));
			counter++;
		}
		sb.append(getString("read.tail", user.getProperties()));
		if (BooleanProperty.booleanValue((String) user.get(UPROP_TEXT_PAGER))) {
			sb.append(Utils.tag(TagConsts.PAGING));
		}
		user.send(sb.toString());
	}

	private boolean skipWhenSearchingByAuthor(Integer authorId) {
		return (onlyForAuthorId != null && !authorId.equals(onlyForAuthorId));
	}
}
